package org.example.controller;

import org.example.config.PropertiesConfig;
import org.example.param.UserFuzzyParam;
import org.example.param.UserRegisterParam;
import org.example.param.UserUpdateParam;
import org.example.param.UserUpdatePwdParam;
import org.example.service.UserService;
import org.example.utils.FileUploadUtil;
import org.example.utils.HttpCode;
import org.example.utils.Result;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 查询出所有user信息
     * @return
     */
    @RequestMapping("/all")
    @PreAuthorize("hasAnyRole('admin')")
    public Result all(){
        return userService.all();
    }

    @PostMapping("/findUserById")
    @PreAuthorize("hasAnyRole('admin')")
    public Result findUserById(@RequestBody Map<String,Long> map){
        Long id = map.get("id");
        if (id == null || id <= 0){
            return Result.fail(HttpCode.PARAM_ERROR);
        }
        return userService.findUserById(id);
    }

    /**
     * 注册
     */
    @PostMapping("/register")
    @PreAuthorize("permitAll()")
    public Result register(@RequestBody @Validated UserRegisterParam userRegisterParam){
        return userService.register(userRegisterParam);
    }

    /**
     * 删除用户
     */
    @PostMapping("/delete")
    @PreAuthorize("hasAnyRole('admin')")
    public Result delete(@RequestBody Map<String,Long> map){
        Long id = map.get("id");
        if (id == null || id <= 0){
            return Result.fail(HttpCode.PARAM_ERROR);
        }
        return userService.delete(id);
    }

    /**
     * 修改用户信息
     */
    @PostMapping("update")
    @PreAuthorize("hasAnyRole('admin','normal')")
    public Result update(@RequestBody UserUpdateParam userUpdateParam){
        return userService.update(userUpdateParam);
    }

    /**
     * 修改密码
     */
    @PostMapping("updatePassword")
    @PreAuthorize("hasAnyRole('admin','normal')")
    public Result updatePassword(@RequestBody @Validated UserUpdatePwdParam userUpdatePwdParam){
        return userService.updatePassword(userUpdatePwdParam);
    }

    /**
     * 模糊查询user信息
     * account
     */
    @PostMapping("/fuzzy")
    @PreAuthorize("hasAnyRole('admin')")
    public Result fuzzy(@RequestBody UserFuzzyParam userFuzzyParam){
        return userService.fuzzy(userFuzzyParam);
    }

    // 只使用userFuzzyParam中的account字段接收属性
    @PostMapping("/getUserByName")
    @PreAuthorize("hasAnyRole('admin')")
    public Result getUserByName(@RequestBody UserFuzzyParam userFuzzyParam){

        return userService.getUserByName(userFuzzyParam.getAccount());
    }

    /**
     * 获取当前用户
     */
    @PostMapping("currentUser")
    @PreAuthorize("hasAnyRole('admin','normal')")
    public Result currentUser(){
        return userService.currentUser();
    }

    @PostMapping("uploadAvatar")
    @PreAuthorize("hasAnyRole('admin','normal')")
    public Result uploadAvatar(@RequestParam("image") MultipartFile file) throws IOException {
        String uploadUrl = FileUploadUtil.upload(PropertiesConfig.getUploadAvatarDir(), file, FileUploadUtil.IMAGE_ALLOWED_EXTENSION);
        return Result.success(FileUploadUtil.transformUrl(uploadUrl));
    }

    @PostMapping("updateAvatar")
    @PreAuthorize("hasAnyRole('admin','normal')")
    public Result updateAvatar(@RequestBody Map<String,String> map){
        String avatar = map.get("avatar");
        return userService.updateAvatar(avatar);
    }
}
